// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';

export default class extends think.Controller {
  /**
   * index action
   * @return {Promise} []
   */
  // 获取首页的数据
  async getWeiXinConfigAction() {
    const data = this.get();
    const res = await this.model('or_weixin_config').where({departmentid: data.departmentid}).find();
    return this.json(res);
  }

  // 获取最新文章
  async getArticleListAction() {
    const data = this.get();
    const res = await this.model('or_weixin_article').page(data.page).where({shopid: data.shopid}).order('id DESC').countSelect();
    return this.json(res);
  }

  /*
   * @desc 获取单个文章信息
   * @return {object} 文章信息
   */
  async getArticleInfoAction() {
    const id = this.get().id;
    const article = await this.model('or_weixin_article').where({id: id}).find();
    return this.json(article);
  }

  //门店项目
  async getstoresubjectAction() {
    let that = this
    let data = that.get()
    let where = {
      'ss.shopid': data.shopid,
      'ss.isdisable': 0
    }
    let list = await this.model('or_shop_subject')
        .alias('ss')
        .field('ss.*, s.name, s.remarks')
        .join('think_or_subject s on s.id=ss.subjectid')
        .where(where)
        .select();

    return this.json(list);
  }

  //门店产品
  async getstoreproductAction() {
    let that = this
    let data = that.get()
    let where = {
      'sg.shopid': data.shopid,
      'sg.isdisable': 0,
      'sg.isshop': 0
    }
    let list = await this.model('or_shop_goods')
        .alias('sg')
        .field('sg.*, g.name, g.remarks')
        .join('think_or_goods g on g.id=sg.goodsid')
        .where(where)
        .select();

    return this.json(list);
  }
}
